﻿using System;
using System.Linq;

namespace PhoneNetTools.Dns.Records
{
    /// <summary>
    /// A class representing a question in a DNS request.
    /// </summary>
    public class Question : ResourceRecordBase
    {
        /// <summary>
        /// Gets the length of the whole resource record, in bytes.
        /// </summary>
        public override int Length
        {
            get
            {
                // compute the string length:
                // string length (assuming ASCII!) + one additional byte for the additional length prefix if there's at least one dot (e.g. 2 dots => 3 parts => 3 prefixes), 
                // plus the 0-termination
                var dotCount = Name.Count(o => o == '.');
                var stringLength = Name.Length + (dotCount > 0 ? 1 : 0) + 1;

                // string length + Type + Class
                return stringLength + sizeof(UInt16) + sizeof(UInt16);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Question"/> class using the given type, and sets the class to <c>IN</c>.
        /// </summary>
        /// <param name="type">The type.</param>
        public Question(QType type)
            : base()
        {
            Type = type;
        }
    }
}
